package com.yzkj.cpjxc.business.service.impl;

import com.yzkj.cpjxc.business.domain.DicObj;
import com.yzkj.cpjxc.business.service.DicObjService;
import com.yzkj.cpjxc.business.framework.common.BusinessMap;
import com.yzkj.cpjxc.business.framework.common.Constants;
import com.yzkj.cpjxc.business.framework.common.Pager;
import com.yzkj.cpjxc.business.framework.utils.IDUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 字典公共管理Service 实现
 */
@Service
public class DicObjServiceImpl implements DicObjService {

	/**
	 * MyBatis 命名空间
	 */
	private final static String MAPPER_NAMESPACE = "com.chkj.dictionary.dicObj.mappers.DicObjMapper";

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	@Transactional
	@Override
	public BusinessMap create(DicObj dicObj) throws Exception {
		BusinessMap bm = new BusinessMap();
		if (this.isExists(dicObj)) {
			bm.put(Constants.WARNING_MSG, "该名称(或代码)已经存在");
			return bm;
		}
		dicObj.setUuid(IDUtils.genUUID());
		Date date = new Date();
		dicObj.setCreateTime(date);
		dicObj.setUpdateTime(date);
		dicObj.setIsActive(Constants.ACTIVE);
		
		//编码中不能带有"-"字符
		if (StringUtils.isNotBlank(dicObj.getDicCode()) && StringUtils.contains(dicObj.getDicCode(), "-")) {
			bm.put(Constants.WARNING_MSG, "编码中不能带有横杠'-'字符");
			return bm;
		}
		
		sqlSessionTemplate.insert(MAPPER_NAMESPACE + ".add", dicObj);
		return bm;
	}

	@Override
	public boolean isExists(DicObj dicObj) throws Exception {
		return (Integer)sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".isExists", dicObj) > 0;
	}
	
	@Override
	public DicObj getById(String uuid) throws Exception {
		return sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getById", uuid);
	}
	
	@Transactional
	@Override
	public BusinessMap edit(DicObj dicObj) throws Exception {
		BusinessMap bm = new BusinessMap();
		if (this.isExists(dicObj)) {
			bm.put(Constants.WARNING_MSG, "该名称(或代码)已经存在");
			return bm;
		}
		
		Date date = new Date();
		dicObj.setUpdateTime(date);
		
		sqlSessionTemplate.update(MAPPER_NAMESPACE + ".edit", dicObj);
		return bm;
	}

	@Transactional
	@Override
	public boolean delete(String uuid) throws Exception {
		sqlSessionTemplate.delete(MAPPER_NAMESPACE + ".falseDeleteById", uuid);
		return true;
	}

	@Override
	public Pager<DicObj> search(Pager<DicObj> pager, DicObj dicObj) throws Exception {
		List<DicObj> dataList = sqlSessionTemplate.selectList(MAPPER_NAMESPACE + ".page", dicObj, new RowBounds((pager.getCurrentPage() - 1) * pager.getLimit(), pager.getLimit()));
		pager.setDataList(dataList);
		
		// 如果需要转换成ShowVo展示，那么就在这里转换
		
		Long total = sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getTotal", dicObj);
		pager.setTotal(total);
		return pager;
	}
	
	@Override
	public List<DicObj> getObjectList(String dicType) throws Exception {
		List<DicObj> dataList = sqlSessionTemplate.selectList(MAPPER_NAMESPACE + ".getObjectList", dicType);
		return dataList;
	}

	@Override
	public DicObj getByDicName(DicObj dicObj) throws Exception {
		return sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getByDicName", dicObj);
	}

}
